package com.tedu._05springmvc2.controller;

import com.tedu._05springmvc2.mapper.UserMapper;
import com.tedu._05springmvc2.pojo.dto.UserDTO;
import com.tedu._05springmvc2.pojo.dto.UserUpdateDTO;
import com.tedu._05springmvc2.pojo.entity.User;
import com.tedu._05springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/users/regUser")
//    @ResponseBody
    public String reg(UserDTO userDTO){
        System.out.println("开始用户注册功能");
        System.out.println(userDTO);

        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        int num = userMapper.insert(user);
        return num>0?"注册成功":"注册失败";
    }

    @RequestMapping("/users/deleteUser")
//    @ResponseBody
    public String delete(Integer id){
        System.out.println("开始处理用户删除...");
        System.out.println("id"+id);
        int num = userMapper.deleteById(id);
        return num>0?"用户删除成功":"用户删除失败";
    }

    @RequestMapping("/users/updateUser")
//    @ResponseBody
    public String update(UserUpdateDTO userUpdateDTO){
        System.out.println("开始处理用户更新...");
        System.out.println(userUpdateDTO);
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        int num = userMapper.updateById(user);
        return num>0?"用户更新成功":"用户更新失败";
    }

    @RequestMapping("/users/findAll")
//    @ResponseBody
    public List<UserVO> findAll(){
        System.out.println("查询所有用户信息");
        return userMapper.selectAll();
    }
}
